#include<iostream>
#include<string>
using namespace std;
string s;
int n;
int main()
{
	int t;
	cin >> t;
	while (t--) {
		cin >> s;
		bool flag = 0;
		for (int i = 1; i < s.size(); i++) {
			if (s[i] != s[i - 1]) {
				flag = 1;
				break;
			}
		}
		if (flag) {
			cout << s.size() - 1 << endl;
		}
		else cout << -1 << endl;
	}
	return 0;
}